package leetcode;

public class Demo05 {
    public static void main(String[] args) {
        System.out.println(longestPalindrome("ac"));
    }

    public static String longestPalindrome(String s) {
        StringBuilder str = new StringBuilder("");
        for (int k = s.length() - 1; k >= 0; k--) {
            str.append(s.charAt(k));
        }
        //找到s与str的最长公共子串，且位置相等
        int i = 0;  //开始索引
        int j = s.length();  //结束索引
        while (true) {
            if (s.substring(i, j).equals(str.substring(i, j))) {
                break;
            }
            if (s.charAt(i) != str.charAt(i)) {
                i++;
            }
            if (s.charAt(j - 1) != str.charAt(j - 1)) {
                j--;
            }
        }
        return s.substring(i,j);
    }
}

